package com.hongxun.project.rest;

import com.alibaba.fastjson.JSON;
import com.hongxun.common.utils.StringUtils;
import com.hongxun.framework.web.controller.BaseController;
import com.hongxun.framework.web.domain.AjaxResult;
import com.hongxun.project.assets.b10kvLineInfo.service.IB10kvLineInfoService;
import com.hongxun.project.assets.bByq.domain.BByq;
import com.hongxun.project.assets.bByq.service.IBByqService;
import com.hongxun.project.assets.bDivice.domain.BDivice;
import com.hongxun.project.assets.bDivice.service.IBDiviceService;
import com.hongxun.project.assets.bMaterial.domain.BMaterial;
import com.hongxun.project.assets.bMaterial.service.IBMaterialService;
import com.hongxun.project.assets.bModel.domain.BModel;
import com.hongxun.project.assets.bModel.service.IBModelService;
import com.hongxun.project.assets.bModelItem.domain.BModelItem;
import com.hongxun.project.assets.bModelItem.service.IBModelItemService;
import com.hongxun.project.assets.iBoxes.domain.IBoxes;
import com.hongxun.project.assets.iBoxes.service.IIBoxesService;
import com.hongxun.project.assets.iCablePit.domain.ICablePit;
import com.hongxun.project.assets.iCablePit.service.IICablePitService;
import com.hongxun.project.assets.iDevice.domain.IDevice;
import com.hongxun.project.assets.iDevice.service.IIDeviceService;
import com.hongxun.project.assets.iLine.domain.ILine;
import com.hongxun.project.assets.iLine.service.IILineService;
import com.hongxun.project.assets.iMaterial.domain.IMaterial;
import com.hongxun.project.assets.iMaterial.service.IIMaterialService;
import com.hongxun.project.assets.iPicture.domain.IPicture;
import com.hongxun.project.assets.iPicture.service.IIPictureService;
import com.hongxun.project.assets.iPoint.domain.IPoint;
import com.hongxun.project.assets.iPoint.service.IIPointService;
import com.hongxun.project.assets.iPole.domain.IPole;
import com.hongxun.project.assets.iPole.service.IIPoleService;
import com.hongxun.project.assets.iPullLine.domain.IPullLine;
import com.hongxun.project.assets.iPullLine.service.IIPullLineService;
import com.hongxun.project.assets.kBj.domain.KBj;
import com.hongxun.project.assets.kBj.service.IKBjService;
import com.hongxun.project.assets.kCj.domain.KCj;
import com.hongxun.project.assets.kCj.service.IKCjService;
import com.hongxun.project.assets.kHgq.domain.KHgq;
import com.hongxun.project.assets.kHgq.service.IKHgqService;
import com.hongxun.project.assets.kPdKg.domain.KPdKg;
import com.hongxun.project.assets.kPdKg.service.IKPdKgService;
import com.hongxun.project.assets.pBoxes.domain.PBoxes;
import com.hongxun.project.assets.pBoxes.service.IPBoxesService;
import com.hongxun.project.assets.pCablePit.domain.PCablePit;
import com.hongxun.project.assets.pCablePit.service.IPCablePitService;
import com.hongxun.project.assets.pDevice.domain.PDevice;
import com.hongxun.project.assets.pDevice.service.IPDeviceService;
import com.hongxun.project.assets.pMaterial.domain.PMaterial;
import com.hongxun.project.assets.pMaterial.service.IPMaterialService;
import com.hongxun.project.assets.pPicture.domain.PPicture;
import com.hongxun.project.assets.pPicture.service.IPPictureService;
import com.hongxun.project.assets.pPoint.domain.PPoint;
import com.hongxun.project.assets.pPoint.service.IPPointService;
import com.hongxun.project.assets.pPole.domain.PPole;
import com.hongxun.project.assets.pPole.service.IPPoleService;
import com.hongxun.project.assets.pPullLine.domain.PPullLine;
import com.hongxun.project.assets.pPullLine.service.IPPullLineService;
import com.hongxun.project.assets.pTask.domain.PTask;
import com.hongxun.project.assets.pTask.service.IPTaskService;
import com.hongxun.project.system.dict.domain.DictData;
import com.hongxun.project.system.dict.service.IDictDataService;
import com.hongxun.project.system.user.domain.User;
import com.hongxun.project.system.user.service.IUserService;
import com.hongxun.project.tool.assets.I2PUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created by admin on 2019/3/12.
 */
@RestController
@RequestMapping("/clear")
public class AppLoginController extends BaseController {

    @Autowired
    IDictDataService dictDataService;
    @Autowired
    IUserService userService;
    @Autowired
    IILineService lineService;
    @Autowired
    IBByqService byqService;
    @Autowired
    IPTaskService taskService;
    @Autowired
    IBMaterialService materialService;
    @Autowired
    IBDiviceService diviceService;
    @Autowired
    IPBoxesService ipBoxesService;
    @Autowired
    IPCablePitService ipCablePitService;
    @Autowired
    IPDeviceService ipDeviceService;
    @Autowired
    IPMaterialService ipMaterialService;
    @Autowired
    IPPointService ipPointService;
    @Autowired
    IPPoleService ipPoleService;
    @Autowired
    IPPullLineService ipPullLineService;
    @Autowired
    private IPPictureService ipPictureService;

    @Autowired
    IIBoxesService iiBoxesService;
    @Autowired
    IICablePitService iiCablePitService;
    @Autowired
    IIDeviceService iiDeviceService;
    @Autowired
    IIMaterialService iiMaterialService;
    @Autowired
    IIPointService iiPointService;
    @Autowired
    IIPoleService iiPoleService;
    @Autowired
    IIPullLineService iiPullLineService;
    @Autowired
    IIPictureService iiPictureService;

    @Autowired
    IKBjService ikBjService;
    @Autowired
    IKHgqService ikHgqService;
    @Autowired
    IB10kvLineInfoService ib10kvLineInfoService;
    @Autowired
    IKCjService ikCjService;
    @Autowired
    IKPdKgService ikPdKgService;
    @Autowired
    IBModelService ibModelService;
    @Autowired
    IBModelItemService ibModelItemService;

    @PostMapping("/logins")
    public AjaxResult login(String username, String password) {
        UsernamePasswordToken token = new UsernamePasswordToken(username, password, true);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            List<DictData> dictData = dictDataService.selectDictDataList(null);
            List<BMaterial> bMaterials = materialService.selectBMaterialList(null);
            List<BDivice> bDivices = diviceService.selectBDiviceList(null);
            User u = new User();
            u.setUserId(getSysUser().getUserId());
            List<User> users = userService.selectUserList(u);
            return success()
                    .put("user", getSysUser())
                    .put("dictData", dictData)
                    .put("bMaterials", bMaterials)
                    .put("bDevices", bDivices)
                    .put("users", users)
                    .put("b10kv", ib10kvLineInfoService.selectB10kvLineInfoList(null))
                    ;
        } catch (AuthenticationException e) {
            String msg = "用户或密码错误";
            if (StringUtils.isNotEmpty(e.getMessage())) {
                msg = e.getMessage();
            }
            return error(msg);
        }
    }

    @PostMapping("/lineList")
    public AjaxResult getLineList(@RequestParam Map<String, Object> map) {
        ILine line = new ILine();
        if (map.get("name") != null && !StringUtils.isEmpty(map.get("name").toString()))
            line.setName(map.get("name").toString());
        if (map.get("lineType") != null && !StringUtils.isEmpty(map.get("lineType").toString()))
            line.setLineType(map.get("lineType").toString());
        if (map.get("voltage") != null && !StringUtils.isEmpty(map.get("voltage").toString()))
            line.setVoltage(map.get("voltage").toString());
        if (map.get("byqId") != null && !StringUtils.isEmpty(map.get("byqId").toString()))
            line.setByqId(map.get("byqId").toString());
        List<ILine> lines = lineService.selectILineList(line);
        return success().put("lines", lines);
    }

    @PostMapping("/byqList")
    public AjaxResult getByqList(@RequestParam Map<String, Object> map) {
        BByq bByq = new BByq();
        if (map.get("name") != null && !StringUtils.isEmpty(map.get("name").toString()))
            bByq.setName(map.get("name").toString());
        List<BByq> byqs = byqService.selectBByqList(bByq);
        return success().put("byqs", byqs);
    }

    @PostMapping("/createLine")
    public AjaxResult createLine(@RequestParam Map<String, Object> map) {
        ILine line = new ILine();
        if (map.get("name") != null && !StringUtils.isEmpty(map.get("name").toString()))
            line.setName(map.get("name").toString());
        if (map.get("lineType") != null && !StringUtils.isEmpty(map.get("lineType").toString()))
            line.setLineType(map.get("lineType").toString());
        if (map.get("voltage") != null && !StringUtils.isEmpty(map.get("voltage").toString()))
            line.setVoltage(map.get("voltage").toString());
        if (map.get("byqId") != null && !StringUtils.isEmpty(map.get("byqId").toString()))
            line.setByqId(map.get("byqId").toString());
        if (map.get("upLine") != null && !StringUtils.isEmpty(map.get("upLine").toString()))
            line.setUpLineId(map.get("upLine").toString());
        line.setAddUser(Integer.parseInt(getSysUser().getUserId().toString()));
        line.setDatastate("新增");
        line.setAddDate(new Date());
        lineService.insertILine(line);
        return success();
    }

    @PostMapping("/createTask")
    public AjaxResult createTask(@RequestParam Map<String, Object> map) throws ParseException {
        PTask task = new PTask();
        if (map.get("name") != null && !StringUtils.isEmpty(map.get("name").toString()))
            task.setTaskName(map.get("name").toString());
//        if (map.get("lineId") != null && !StringUtils.isEmpty(map.get("lineId").toString()))
//            task.setLineId(map.get("lineId").toString());
        if (map.get("byqId") != null && !StringUtils.isEmpty(map.get("byqId").toString()))
            task.setByqId(map.get("byqId").toString());
        if (map.get("users") != null && !StringUtils.isEmpty(map.get("users").toString()))
            task.setUseId(map.get("users").toString());
        if (map.get("auditors") != null && !StringUtils.isEmpty(map.get("auditors").toString()))
            task.setAuditorId(map.get("auditors").toString());
        if (map.get("sT") != null && !StringUtils.isEmpty(map.get("sT").toString()))
            task.setPStartTime(new SimpleDateFormat("yyyy-MM-dd").parse(map.get("sT").toString()));
        if (map.get("eT") != null && !StringUtils.isEmpty(map.get("eT").toString()))
            task.setPEndTime(new SimpleDateFormat("yyyy-MM-dd").parse(map.get("eT").toString()));
        if (map.get("note") != null && !StringUtils.isEmpty(map.get("note").toString()))
            task.setTaskNote(map.get("note").toString());
        task.setLineId(lineService.getLinesByByqId(task.getByqId()));//设置线路
        task.setCreateId(getSysUser().getUserId() + "");
        task.setTaskStatus("新增");
        taskService.insertPTask(task);
        return success();
    }

    @PostMapping("/getTaskList")
    public AjaxResult getTaskList(@RequestParam Map<String, Object> map) {
        PTask task = new PTask();
        String status = "全部";
        if (map.get("status") != null && !StringUtils.isEmpty(map.get("status").toString()))
            status = map.get("status").toString();
        if (!status.equals("全部")) task.setTaskStatus(status);
        if (map.get("role") != null && !StringUtils.isEmpty(map.get("role").toString()) && map.get("role").toString().equals("采集")) {
            task.setUseId(getSysUser().getUserId() + "");
        } else {
            task.setAuditorId(getSysUser().getUserId() + "");
        }
        List<PTask> tasks = taskService.selectPTaskList(task);
        for (PTask t : tasks) {
            String lineNames = lineService.getLinesNameByqId(t.getByqId());//获取到变压器下的所有线路
            if (StringUtils.isEmpty(lineNames)) {
                t.setLineName("暂无线路");
            } else t.setLineName(lineNames);

        }
        return success().put("tasks", tasks);
    }

    @PostMapping("/updateTaskStatus")
    public AjaxResult updateTaskStatus(@RequestParam Map<String, Object> map) throws InterruptedException {
        PTask task = taskService.selectPTaskById(map.get("id").toString());
        String status = map.get("status").toString();
        task.setTaskStatus(status);
        taskService.updatePTask(task);
        if (status.equals("采集")) {
            copyData2Task(task);
        }
        return success();
    }

    public void copyData2Task(PTask task) throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(20);//获取线程池
        CountDownLatch latch = new CountDownLatch(8);//线程计数器
        IPoint iPoint = new IPoint();
        iPoint.setByqId(task.getByqId());//根据变压器获取线路IDS
        final List<IPoint> iPoints =iiPointService.selectIPointList(iPoint);
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < iPoints.size(); i++) {
            stringBuffer.append(iPoints.get(i).getId());
            if (i != iPoints.size() - 1) stringBuffer.append(",");
        }
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                ipPointService.insertOrReplaceOrDelete(I2PUtils.getPoint(task.getId(), iPoints));
                latch.countDown();
            }
        });
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                IBoxes iBoxes = new IBoxes();
                iBoxes.setPId(stringBuffer.toString());
                ipBoxesService.insertOrReplaceOrDelete(I2PUtils.getBoxes(task.getId(),
                        iiBoxesService.selectIBoxesList(iBoxes)));
                latch.countDown();
            }
        });
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                ICablePit iCablePit = new ICablePit();
                iCablePit.setPId(stringBuffer.toString());
                ipCablePitService.insertOrReplaceOrDelete(I2PUtils.getCablePit(task.getId(),
                        iiCablePitService.selectICablePitList(iCablePit)));
                latch.countDown();
            }
        });
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                IDevice iCablePit = new IDevice();
                iCablePit.setPointId(stringBuffer.toString());
                ipDeviceService.insertOrReplaceOrDelete(I2PUtils.getDevice(task.getId(),
                        iiDeviceService.selectIDeviceList(iCablePit)));
                latch.countDown();
            }
        });
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                IMaterial iCablePit = new IMaterial();
                iCablePit.setPointId(stringBuffer.toString());
                ipMaterialService.insertOrReplaceOrDelete(I2PUtils.getMaterial(task.getId(),
                        iiMaterialService.selectIMaterialList(iCablePit)));
                latch.countDown();
            }
        });
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                IPole iCablePit = new IPole();
                iCablePit.setPId(stringBuffer.toString());
                ipPoleService.insertOrReplaceOrDelete(I2PUtils.getPole(task.getId(),
                        iiPoleService.selectIPoleList(iCablePit)));
                latch.countDown();
            }
        });
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                IPicture iCablePit = new IPicture();
                iCablePit.setPId(stringBuffer.toString());
                ipPictureService.insertOrReplaceOrDelete(I2PUtils.getPicture(task.getId(),
                        iiPictureService.selectIPictureList(iCablePit)));
                latch.countDown();
            }
        });
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                IPullLine iCablePit = new IPullLine();
                iCablePit.setPId(stringBuffer.toString());
                ipPullLineService.insertOrReplaceOrDelete(I2PUtils.getPullLine(task.getId(),
                        iiPullLineService.selectIPullLineList(iCablePit)));
                latch.countDown();
            }
        });
        latch.await();
        executorService.shutdown();
    }


    @PostMapping("/downloadTask")
    public AjaxResult downloadTask(@RequestParam Map<String, Object> map) throws InterruptedException {
        final AjaxResult result = success();
        ExecutorService executorService = Executors.newFixedThreadPool(20);//获取线程池
        PTask task = JSON.parseObject(map.get("task").toString(), PTask.class);
        String ids = lineService.getLinesByByqId(task.getByqId());//获取线路的IDS
        task.setLineId(ids);//将线路设备到任务中去，线路信息一直处于更新的状态
        String nameByqId = lineService.getLinesNameByqId(task.getByqId());
        if (StringUtils.isEmpty(nameByqId)) nameByqId = "暂无线路";
        task.setLineName(nameByqId);//设置线路名称
        CountDownLatch latch = new CountDownLatch(14);//线程计数器
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                PPoint pPoint = new PPoint();
                pPoint.setTaskId(task.getId());
                result.put("point", ipPointService.selectPPointList(pPoint));
                latch.countDown();
            }
        });
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                result.put("byq", byqService.selectBByqById(task.getByqId()));
                latch.countDown();
            }
        });
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                ILine iLine = new ILine();
                iLine.setId(ids);
                result.put("line", lineService.selectILineList(iLine));
                latch.countDown();
            }
        });
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                PBoxes iBoxes = new PBoxes();
                iBoxes.setTaskId(task.getId());
                result.put("boxes", ipBoxesService.selectPBoxesList(iBoxes));
                latch.countDown();
            }
        });
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                PCablePit iCablePit = new PCablePit();
                iCablePit.setTaskId(task.getId());
                result.put("cablePit", ipCablePitService.selectPCablePitList(iCablePit));
                latch.countDown();
            }
        });
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                PDevice iCablePit = new PDevice();
                iCablePit.setTaskId(task.getId());
                result.put("device", ipDeviceService.selectPDeviceList(iCablePit));
                latch.countDown();
            }
        });
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                PMaterial iCablePit = new PMaterial();
                iCablePit.setTaskId(task.getId());
                result.put("material", ipMaterialService.selectPMaterialList(iCablePit));
                latch.countDown();
            }
        });
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                PPole iCablePit = new PPole();
                iCablePit.setTaskId(task.getId());
                result.put("pole", ipPoleService.selectPPoleList(iCablePit));
                latch.countDown();
            }
        });
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                PPicture iCablePit = new PPicture();
                iCablePit.setTaskId(task.getId());
                result.put("picture", ipPictureService.selectPPictureList(iCablePit));
                latch.countDown();
            }
        });
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                PPullLine iCablePit = new PPullLine();
                iCablePit.setTaskId(task.getId());
                result.put("pullLine", ipPullLineService.selectPPullLineList(iCablePit));
                latch.countDown();
            }
        });
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                KBj k = new KBj();
                k.setByqId(task.getByqId());
                List<KBj> kBjs = ikBjService.selectKBjList(k);
                for (KBj kBj : kBjs) kBj.setDatastate("已同步");
                result.put("kbj", kBjs);
                latch.countDown();
            }
        });
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                KHgq k = new KHgq();
                k.setByqId(task.getByqId());
                List<KHgq> kHgqs = ikHgqService.selectKHgqList(k);
                for (KHgq kHgq : kHgqs) kHgq.setDatastate("已同步");
                result.put("khgq", kHgqs);
                latch.countDown();
            }
        });
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                KCj k = new KCj();
                k.setByqId(task.getByqId());
                List<KCj> kHgqs = ikCjService.selectKCjList(k);
                for (KCj kHgq : kHgqs) kHgq.setDatastate("已同步");
                result.put("kcj", kHgqs);
                latch.countDown();
            }
        });
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                KPdKg k = new KPdKg();
                k.setByqId(task.getByqId());
                List<KPdKg> kHgqs = ikPdKgService.selectKPdKgList(k);
                for (KPdKg kHgq : kHgqs) kHgq.setDatastate("已同步");
                result.put("kg", kHgqs);
                latch.countDown();
            }
        });
        latch.await();
        executorService.shutdown();
        return result.put("task", task);
    }

    @PostMapping("/upData")
    public AjaxResult upData(@RequestParam Map<String, Object> map) throws InterruptedException {
        PTask task = taskService.selectPTaskById(map.get("task").toString());
        if (!task.getTaskStatus().equals("采集")) {
            return error("该任务已进入审核状态，请联系管理员。");
        }
        CountDownLatch latch = new CountDownLatch(13);//线程计数器
        ExecutorService executorService = Executors.newFixedThreadPool(20);//获取线程池
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                List<ILine> boxes = JSON.parseArray(map.get("lines").toString(), ILine.class);
                lineService.insertOrReplaceOrDelete(boxes);
                latch.countDown();
            }
        });
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                List<PBoxes> boxes = JSON.parseArray(map.get("boxes").toString(), PBoxes.class);
                ipBoxesService.insertOrReplaceOrDelete(boxes);
                latch.countDown();
            }
        });
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                List<PCablePit> cablePits = JSON.parseArray(map.get("cablePits").toString(), PCablePit.class);
                ipCablePitService.insertOrReplaceOrDelete(cablePits);
                latch.countDown();
            }
        });
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                List<PDevice> devices = JSON.parseArray(map.get("devices").toString(), PDevice.class);
                ipDeviceService.insertOrReplaceOrDelete(devices);
                latch.countDown();
            }
        });
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                List<PMaterial> materials = JSON.parseArray(map.get("materials").toString(), PMaterial.class);
                ipMaterialService.insertOrReplaceOrDelete(materials);
                latch.countDown();
            }
        });
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                List<PPoint> points = JSON.parseArray(map.get("points").toString(), PPoint.class);
                ipPointService.insertOrReplaceOrDelete(points);
                latch.countDown();
            }
        });
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                List<PPole> poles = JSON.parseArray(map.get("poles").toString(), PPole.class);
                ipPoleService.insertOrReplaceOrDelete(poles);
                latch.countDown();
            }
        });
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                List<PPullLine> pullLines = JSON.parseArray(map.get("pullLines").toString(), PPullLine.class);
                ipPullLineService.insertOrReplaceOrDelete(pullLines);
                latch.countDown();
            }
        });
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                BByq bByq = JSON.parseObject(map.get("byq").toString(), BByq.class);
                byqService.updateBByq(bByq);
                latch.countDown();
            }
        });
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                if (map.get("kbjs") != null) {
                    List<KBj> kBjs = JSON.parseArray(map.get("kbjs").toString(), KBj.class);
                    ikBjService.insertOrReplaceOrDelete(kBjs);
                }
                latch.countDown();
            }
        });
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                if (map.get("khgqs") != null) {
                    List<KHgq> kHgqs = JSON.parseArray(map.get("khgqs").toString(), KHgq.class);
                    ikHgqService.insertOrReplaceOrDelete(kHgqs);
                }
                latch.countDown();
            }
        });
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                if (map.get("kcj") != null) {
                    List<KCj> kcjs = JSON.parseArray(map.get("kcj").toString(), KCj.class);
                    ikCjService.insertOrReplaceOrDelete(kcjs);
                }
                latch.countDown();
            }
        });
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                if (map.get("kg") != null) {
                    List<KPdKg> kPdKgs = JSON.parseArray(map.get("kg").toString(), KPdKg.class);
                    ikPdKgService.insertOrReplaceOrDelete(kPdKgs);
                }
                latch.countDown();
            }
        });
        latch.await();
        executorService.shutdown();
        task.setLineId(lineService.getLinesByByqId(task.getByqId()));
        taskService.updatePTask(task);//给task重新设置线路IDS
        return success().put("task", task);
    }

    @PostMapping("/updateModel")
    public AjaxResult updateModel(@RequestParam Map<String, Object> map) {
        List<BModel> models = JSON.parseArray(map.get("models").toString(), BModel.class);
        List<BModelItem> modelItems = JSON.parseArray(map.get("modelItems").toString(), BModelItem.class);
        ibModelService.insertOrReplaceOrDelete(models);
        ibModelItemService.insertOrReplaceOrDelete(modelItems);
        BModel b = new BModel();
        b.setUserId(Integer.parseInt(getUserId().toString()));
        b.setRange("系统");
        List<BModel> bModels = ibModelService.selectBModelList(b);
        List<BModelItem> bModelItems = new ArrayList<>();
        for (BModel m : bModels) {
            BModelItem i = new BModelItem();
            i.setModelId(m.getId());
            List<BModelItem> bModelItems1 = ibModelItemService.selectBModelItemList(i);
            bModelItems.addAll(bModelItems1);
        }
        return success().put("models", bModels).put("modelItems", bModelItems);
    }
}
